package com.kehutong.payfees.app;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import com.kehutong.common.DoveClient;
import com.kehutong.common.TokenService;
import com.kehutong.common.util.Token;
import com.kehutong.payfees.dao.ChargeBillDao;
import com.kehutong.payfees.enums.*;
import com.kehutong.payfees.service.ChargeRecordServiceImpl;
import com.kehutong.payfees.service.record.AccoutServiceImpl;
import com.kehutong.payfees.syn.CallBackReturn;
import com.kehutong.payfees.syn.Locks;
import com.kehutong.payfees.util.BasicClient;
import com.kehutong.payfees.util.JDBCUtil;
import org.coraframework.converter.util.StringUtil;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONArray;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.execute.Condition;
import org.coraframework.orm.jdbc.execute.QueryCondition;
import org.coraframework.orm.jdbc.execute.entity.FindArray;
import org.coraframework.util.EnumUtil;
import org.coraframework.util.Objects;

import com.kehutong.common.math.CalculatorUtils;
import com.kehutong.payfees.entity.ChargeBill;
import com.kehutong.payfees.entity.ChargeBillItem;
import com.kehutong.payfees.entity.ChargeSundry;
import com.kehutong.payfees.entity.record.ChargeRecord;
import com.kehutong.payfees.entity.record.ChargeRecordTemporary;
import com.kehutong.payfees.service.ChargeBillServiceImpl;

/**
 * Author:  peter
 * Date:    2020/7/8
 * Function:业主端-缴费模块 账单
 */
@MvcAction
@ReqMapping("/kht-bin/charge/client")
public class ChargeBillAction {

    private static final Logger logger = LoggerFactory.getLogger(ChargeBillAction.class);

    @Inject
    private DoveClient doveClient;
    @Inject
    private BasicClient basicClient;
    @Inject
    private ChargeBillServiceImpl chargeBillService;
    @Inject
    private AccoutServiceImpl accoutService;
    @Inject
    private TokenService tokenService;
    @Inject
    private ChargeRecordServiceImpl chargeRecordService;
    @Inject
    private JdbcSession jdbcSession;
    @Inject
    private ChargeBillDao chargeBillDao;

    /**
     * 账单列表
     */
    @ReqMapping("/bill/list")
    public Object listBill(Token token, JSONObject req) {

        if (StringUtil.isBlank(req.getString("houseNo"))) {
            return Result.error(0, "请选择房屋");
        }

        // 找到一条未删除的绑定记录
//        CustomerHouse ch = jdbcSession.findOne(CustomerHouse.class)
//                .eq("houseId", req.getString("houseNo"))
//                .eq("contactId", session.getUserId())
//                .eq("deleted", false)
//
//                .exe();
//        if (ch == null) {
//            return Result.error(0, "您未申请绑定该房屋");
//        }

        FindArray<ChargeBill> findBills = jdbcSession.findArray(ChargeBill.class)
                .eq(req, "houseNo", "paidState");

        JDBCUtil.between(req, findBills, "shouldPayDate");

        if (BillPaidState.PAID == EnumUtil.getEnum(BillPaidState.class, req.getString("paidState"))) {
            // 非业主 查看绑定后产生的已付款记录
//            if (!CustomerType.OWNER.equals(ch.getUserType())) {
//                findBills.maxEq("paidAt", ch.getCreateTime());
//            }
        }

        List<ChargeBill> bills = findBills
                .eq("enableState", BillEnableState.ENABLE)
                .eq("deleted", false)
                .exe();

        for (ChargeBill bill : bills) {
            if (bill.getShouldPayDate() != null) {
                bill.setBillYear(bill.getShouldPayDate().getYear());
                bill.setBillMonth(bill.getShouldPayDate().getMonthValue());
            }
        }

        return Result.success(bills);
    }

    /**
     * 小程序端 统计当前房屋待缴金额
     * @param request
     * @param jsonObject
     * @return
     * @throws Exception
     */
    @ReqMapping("/getUnPaidTotal")
    public Object getUnPaidTotal(HttpServletRequest request, JSONObject jsonObject) throws Exception {
        Token token = tokenService.getUserToken(request);

        if (!checkLogin(token)) {
            return Result.error(0, "请先登录");
        }

        QueryCondition query = Condition.createQuery(false);

        String houseNo = jsonObject.getString("houseNo");
        if(Objects.nonEmpty(houseNo)){
            query.eq("houseNo", houseNo);
        }

        query.eq("enableState", BillEnableState.ENABLE);
        query.eq("deleted", false);

        return Result.success(chargeBillDao.getUnPaidTotal(query));
    }


    /**
     * 小程序端 获取当前房屋下某年某月的账单明细信息
     * @param request
     * @param jsonObject
     * @return
     * @throws Exception
     */
    @ReqMapping("/getBillItems")
    public Object getBillItems(HttpServletRequest request, JSONObject jsonObject) throws Exception {
        Token token = tokenService.getUserToken(request);

        if (!checkLogin(token)) {
            return Result.error(0, "请先登录");
        }

        QueryCondition query = Condition.createQuery(false);

        String yearMonth = jsonObject.getString("yearMonth");
        if(Objects.nonEmpty(yearMonth)){
            query.eq("yearMonth", yearMonth);
        }

        String houseNo = jsonObject.getString("houseNo");
        if(Objects.nonEmpty(houseNo)){
            query.eq("houseNo", houseNo);
        }

        String paidState = jsonObject.getString("paidState");
        if(Objects.nonEmpty(paidState)){
            query.eq("paidState", paidState);
        }

        query.eq("deleted", false);
        query.eq("enableState", BillEnableState.ENABLE);
        List<Map<String,Object>> billMaps = chargeBillDao.getBillIds(query);
        Set<String> ids = billMaps.stream().map(key -> String.valueOf(key.get("id")) ).collect(Collectors.toSet());

        List<ChargeBillItem> items = jdbcSession.findArray(ChargeBillItem.class)
                .in("billNo", ids)
                .exe();

        return Result.success(items);
    }

    /**
     * 小程序端 聚合账单列表
     * @param request
     * @param jsonObject
     * @return
     * @throws Exception
     */
    @ReqMapping("/merge/list")
    public Object mergeBills(HttpServletRequest request, JSONObject jsonObject) throws Exception {

        Token token = tokenService.getUserToken(request);

        if (!checkLogin(token)) {
            return Result.error(0, "请先登录");
        }

        QueryCondition query = Condition.createQuery(false);

        // true 未缴清的数据
        String isUnPaid = jsonObject.getString("isUnPaid");
        if(Objects.nonEmpty(isUnPaid) && Boolean.valueOf(isUnPaid)){
            query.max("unPaidTotal", 0);
        }

        String year = jsonObject.getString("year");
        if(Objects.nonEmpty(year)){
            query.eq("year", year);
        }

        String houseNo = jsonObject.getString("houseNo");
        if(Objects.nonEmpty(houseNo)){
            query.eq("houseNo", houseNo);
        }

        JSONArray yearMonth = jsonObject.getJSONArray("yearMonth");
        if(Objects.nonEmpty(yearMonth)){
            query.in("yearMonth", yearMonth);
        }

        query.order("yearMonth","desc");

        return Result.success(chargeBillDao.getMergeBills(query));
    }

    /**
     * 账单详情
     */
    @ReqMapping("/bill/get")
    public Object getBill(HttpServletRequest request, JSONObject jsonObject) throws Exception {

        Token token = tokenService.getUserToken(request);

        if (!checkLogin(token)) {
            return Result.error(0, "请先登录");
        }
        token.setCompanyNo(request.getHeader("companyNo"));

        String houseNo = jsonObject.getString("houseNo");
        String billNo = jsonObject.getString("billNo");

        ChargeBill bill = Pool.get(ChargeBill.class, billNo);
        if (bill == null) {
            return Result.error(0, "账单不存在");
        }

        JSONObject house = basicClient.getHouseById(token, houseNo);

        List<ChargeBillItem> billItems = jdbcSession.findArray(ChargeBillItem.class)
                .eq("billNo", bill.getId())
                .exe();
        bill.setBillItems(billItems);

        JSONObject result = new JSONObject();
        result.put("bill", bill);
        result.put("house", house);

        return Result.success(result);
    }

    /**
     * 小程序端
     * 微信支付-微信统一订单
     */
    @ReqMapping("/bill/pay")
    public Object createWxUnifiedOrder(HttpServletRequest request, JSONObject req) throws Exception {

        Token token = tokenService.getUserToken(request);
        if (Objects.isNull(token)) {
            return Result.error(0, "当前用户未登录.");
        }

        token.setCompanyNo(request.getHeader("companyNo"));

        String appId = req.getString("appId");
        if (Objects.isEmpty(appId)) {
            return Result.error(0, "appId不能为空.");
        }

        JSONObject rs = doveClient.post("/kht-bin/wechat/getMemberMap", http -> {
            http.addParam("khtId",token.getUuid());
            http.addParam("mn_app_id",appId);
        });
        int code = rs.getIntValue("code");
        if (code != 200) {
            return Result.error(code, rs.getString("message"));
        }
        JSONObject memberMap = rs.getJSONObject("data");
        if(Objects.isEmpty(memberMap)){
            return Result.error(1,"当前用户没有绑定OpenId.");
        }

        ChargeBill bill = Pool.get(ChargeBill.class, req.getString("billNo"));

        if (bill.getPaidState() == BillPaidState.PAID) {
            return Result.error(0, "账单已支付");
        }
        if (bill.getPaidState() != BillPaidState.UNPAID) {
            return Result.error(0, "当前状态不可支付");
        }
        if (bill.getTotal().compareTo(BigDecimal.ZERO) <= 0) {
            return Result.error(0, "账单异常：账单金额须大于0元");
        }
        if (CalculatorUtils.getDecimalPlace(bill.getTotal()) > 2) {
            return Result.error(0, "账单异常：金额最多支持2位小数");
        }

        BigDecimal amt = bill.getTotal();
        if (amt.compareTo(BigDecimal.ZERO) <= 0) {
            return Result.error(0, "总金额必须大于0");
        }

        Map<String, JSONObject> communityMap = basicClient.getCommunityMap(token);

        JSONObject house = basicClient.getHouseById(token, bill.getHouseNo());

        if (Objects.isNull(house)) {
            return Result.error(0, "房屋不存在");
        }

        ChargeRecord cr = Pool.newInstance(ChargeRecord.class);
        cr.setCommunityNo(house.getString("communityNo"));
        cr.setCommunityName(communityMap.get(house.getString("communityNo")).getString("name"));
        cr.setHouseNo(house.getString("id"));

        chargeRecordService.setDepartmentNo(cr, token.getCompanyNo());
        cr.setMbrCode(token.getUuid());
        cr.setMbrName(token.getName());
        cr.setMobile(token.getMobile());
        cr.setAvatar(token.getHeadUrl());
        cr.setOpenid(memberMap.getString("mn_open_id"));
        cr.setAmt(amt);
        cr.setTradeType("JSAPI");
        cr.setBizType(WaterSource.WUYEFEI);
        
        CallBackReturn<JSONObject> call = ()->{
            JSONObject json = new JSONObject();
            json.put("merge", false);

            return chargeRecordService.createUnifiedOrderByChargeRecord(
                    token,
                    request,
                    cr,
                    "业主端账单支付",
                    json.toJSONString(),"wxpay_mp"
            );
        };

        JSONObject result = Locks.exeSyn(cr.getId(), call);

        if (result.getInteger("code") == 200) {
            // 微信统一下单成功 关联充值记录到缴费任务
            bill.setChargeRecordId(cr.getId());
            jdbcSession.updateById(bill);
            // 创建订单关联 用于回调
            chargeBillService.insertChargeRecordRel(cr.getId(), ChargeRecordRelType.CHARGE_BILL, bill.getId());
        }

        return result;
    }

    /**
     * 业主端-多选账单后
     * 使用预付款支付
     * @throws Exception 
     */
//    @ReqMapping("/bill/pay/advance_pay")
//    public Object payBillByAdvance(Token token, JSONObject req) throws Exception {
//        String contactId = token.getUuid();
//
//        if (Objects.isEmpty(contactId)) {
//            return Result.unlogin();
//        }
//
//        List<ChargeBill> bills = jdbcSession.findArray(ChargeBill.class)
//                .in("id", req.getJSONArray("ids"))
//                .eq("deleted", false)
//                .exe();
//
//        // 传入id及查出记录数量比对
//        if (bills.size() != req.getJSONArray("ids").size()) {
//            return Result.error(0, "账单不存在");
//        }
//
//        List<String> houseNos = bills.stream().map(ChargeBill::getHouseNo)
//                .distinct()
//                .collect(Collectors.toList());
//        if (houseNos.size() != 1) {
//            return Result.error(0, "非同一房屋的账单");
//        }
//        final String houseNo = houseNos.get(0);
//
//        for (ChargeBill bill : bills) {
//            if (bill.getPaidState() == BillPaidState.PAID) {
//                return Result.error(0, "账单已支付");
//            }
//            if (bill.getPaidState() != BillPaidState.UNPAID) {
//                return Result.error(0, "当前状态不可支付");
//            }
//        }
//
//        JSONObject house = basicClient.getHouseById(token, houseNo);
//        if (Objects.isNull(house)) {
//            return Result.error(0, "房屋不存在");
//        }
//
//        // todo 获取付款人 id
//        WebChat c = Pool.get(WebChat.class, contactId);
//        if (c == null) {
//            return Result.error(0, "无效的外部联系人");
//        }
//
//        CallBackReturn<JSONResult> called = ()->{
//        	BigDecimal shouldPayTotal = chargeBillService.sumTotal(bills);
//
//            if (!accoutService.validateAccountAmtIsEnough(house.getString("id"), AccountType.OWNER, shouldPayTotal)) {
//                return Result.error(2001, "余额不足");
//            }
//
//            for (ChargeBill bill : bills) {
//                JSONResult dealResult = accoutService.doPayWuYe(bill.getId(), bill.getHouseNo(), bill.getCommunityNo(), bill.getTotal());
//                if (!dealResult.isOk()) {
//                    return Result.error(2001, "预付款余额不足，部分账单支付失败");
//                }
//
//                bill.setPaidAt(LocalDateTime.now());
//                bill.setPayMethod(BillPayMethod.ADVANCE_PAY);
//                bill.setPaidState(BillPaidState.PAID);
//                // 缴费人信息
//                bill.setPayerNo(c.getId());
//                bill.setPayerName(c.getNickName());
//                bill.setPayerMobile(c.getMobile());
//                jdbcSession.updateById(bill);
//            }
//        	return null;
//        };
//
//        JSONResult result = Locks.exeSyn(house.getString("id"), called);
//        if (result != null) {
//        	return result;
//        }
//
////        chargeBillService.asyncNotifyCompanyAfterPaySuccess(bills);
//        chargeBillService.asyncNotifyCustomAfterPaySuccess(token.getCompanyNo(), bills);
//
//        return Result.success();
//    }

    /**
     * 杂项费用详情
     */
    @ReqMapping("/sundry/get")
    public Object getSundry(String id) {
        ChargeSundry chargeSundry = Pool.get(ChargeSundry.class, id);
        if (Objects.nonNull(chargeSundry)) {
            return Result.success(chargeSundry);
        }

        return Result.error(-1, "杂项费用信息不存在");
    }

    /**
     * 小程序端 支付成功后调用缴费明细详情，根据年月进行分组操作
     * @param jsonObject
     * @param request
     * @return
     * @throws Exception
     */
    @ReqMapping("/group/payment/detail")
    public Object groupPaymentDetail(JSONObject jsonObject, HttpServletRequest request) throws Exception {
        Token token = tokenService.getUserToken(request);
        if (Objects.isNull(token)) {
            return Result.error(0, "当前用户未登录.");
        }

        //支付订单号
        String orderNo = jsonObject.getString("orderNo");

        List<ChargeBill> bills = jdbcSession.findArray(ChargeBill.class)
                .eq("chargeRecordId" , orderNo)
                .exe();

        Map<String, List<ChargeBill>> groupMap = new HashMap<>();

        for (ChargeBill bill : bills) {
            if (bill.getShouldPayDate() != null) {
                String year = String.valueOf(bill.getShouldPayDate().getYear());

                String month = bill.getShouldPayDate().getMonthValue() > 9
                        ? String.valueOf(bill.getShouldPayDate().getMonthValue())
                        : "0" + bill.getShouldPayDate().getMonthValue();

                bill.setBillYear(bill.getShouldPayDate().getYear());
                bill.setBillMonth(bill.getShouldPayDate().getMonthValue());

                List<ChargeBill> group = groupMap.get(year + "-" + month);
                if(null == group){
                    group = new ArrayList<>();
                }

                group.add(bill);
                groupMap.put(year + "-" + month, group);
            }
        }

        groupMap = sortMapByKey(groupMap);

        JSONArray resultJA = new JSONArray();
        if(Objects.nonEmpty(groupMap)){
            groupMap.forEach((k, v)->{
                BigDecimal total = chargeBillService.sumTotal(v);

                JSONObject billGroup = new JSONObject();

                String[] key = k.split("-");

                List<ChargeBillItem> items = new ArrayList<>();
                v.forEach(item->{
                    items.addAll(jdbcSession.findArray(ChargeBillItem.class)
                            .eq("billNo", item.getId())
                            .exe());
                });

                billGroup.put("year", key[0]);
                billGroup.put("month", key[1]);
                billGroup.put("items", items);
                billGroup.put("total", total);
                resultJA.add(billGroup);
            });
        }

        return Result.success(resultJA);
    }

    /**
     * 使用 Map按key进行排序
     * @param map
     * @return
     */
    public static Map<String, List<ChargeBill>> sortMapByKey(Map<String, List<ChargeBill>> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }

        Map<String, List<ChargeBill>> sortMap = new TreeMap<String, List<ChargeBill>>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });

        sortMap.putAll(map);

        return sortMap;
    }

    /**
     * 小程序端 聚合支付
     * @param jsonObject
     * @param request
     * @return
     * @throws Exception
     */
    @ReqMapping("/merge/pay")
    public Object mergePay(JSONObject jsonObject, HttpServletRequest request) throws Exception {
        Token token = tokenService.getUserToken(request);
        if (Objects.isNull(token)) {
            return Result.error(0, "当前用户未登录.");
        }
        token.setCompanyNo(request.getHeader("companyNo"));

        String appId = jsonObject.getString("appId");
        if (Objects.isEmpty(appId)) {
            return Result.error(0, "appId不能为空.");
        }
        String houseNo = jsonObject.getString("houseNo");
        JSONArray yearMonth = jsonObject.getJSONArray("yearMonth");

        JSONObject house = basicClient.getHouseById(token, houseNo);

        ChargeRecord cr = Pool.newInstance(ChargeRecord.class);
        cr.setCommunityNo(house.getString("communityNo"));
        cr.setCommunityName(house.getString("communityName"));
        cr.setHouseNo(house.getString("id"));
        JSONObject rs = doveClient.post("/kht-bin/wechat/getMemberMap", http -> {
            http.addParam("khtId",token.getUuid());
            http.addParam("mn_app_id",appId);
        });
        int code = rs.getIntValue("code");
        if (code != 200) {
            return Result.error(code, rs.getString("message"));
        }
        JSONObject memberMap = rs.getJSONObject("data");
        if(Objects.isEmpty(memberMap)){
            return Result.error(1,"当前用户没有绑定OpenId.");
        }

        chargeRecordService.setDepartmentNo(cr, token.getCompanyNo());

        cr.setMbrCode(token.getUuid());
        cr.setMbrName(token.getName());
        cr.setMobile(token.getMobile());
        cr.setAvatar(token.getHeadUrl());
        cr.setOpenid(memberMap.getString("mn_open_id"));


        QueryCondition query = Condition.createQuery(false);
        query.in("yearMonth", yearMonth);
        query.eq("houseNo", houseNo);
        query.eq("paidState", BillPaidState.UNPAID);
        query.eq("deleted", false);
        query.eq("enableState", BillEnableState.ENABLE);

        // 获取所选年月所有 待缴费 账单信息
        List<Map<String,Object>> billMaps = chargeBillDao.getBillIds(query);

        Set<String> ids = billMaps.stream().map(key -> String.valueOf(key.get("id")) ).collect(Collectors.toSet());

        List<ChargeBill> bills = jdbcSession.findArray(ChargeBill.class)
                .in("id", ids)
                .eq("deleted", false)
                .exe();

        //获取代缴总金额
        BigDecimal amt = chargeBillService.sumTotal(bills);
        cr.setAmt(amt);

        cr.setTradeType("JSAPI");
        cr.setBizType(WaterSource.WUYEFEI);

        CallBackReturn<JSONObject> called = ()->{

            JSONObject json = new JSONObject();
            json.put("merge", true);

            return chargeRecordService.createUnifiedOrderByChargeRecord(
                    token,
                    request,
                    cr,
                    "合并支付",
                    json.toJSONString(),"wxpay_mp"
            );
        };
        JSONObject result = Locks.exeSyn(houseNo, called);

        if (result.getInteger("code") == 200) {
            // 微信统一下单成功，记录相关信息
            for (ChargeBill bill : bills) {
                bill.setChargeRecordId(cr.getId());
                jdbcSession.updateById(bill);
                // 创建订单关联 用于回调
                chargeBillService.insertChargeRecordRel(cr.getId(), ChargeRecordRelType.CHARGE_BILL, bill.getId());
            }
        }
        return result;

    }

    @ReqMapping("/sundry/pay")
    public Object sundryPay(JSONObject jsonObject, HttpServletRequest request) throws Exception {
        Token token = tokenService.getUserToken(request);
        if (Objects.isNull(token)) {
            return Result.error(0, "当前用户未登录.");
        }

        token.setCompanyNo(request.getHeader("companyNo"));

        String appId = jsonObject.getString("appId");
        if (Objects.isEmpty(appId)) {
            return Result.error(0, "appId不能为空.");
        }

        JSONObject rs = doveClient.post("/kht-bin/wechat/getMemberMap", http -> {
            http.addParam("khtId",token.getUuid());
            http.addParam("mn_app_id",appId);
        });
        int code = rs.getIntValue("code");
        if (code != 200) {
            return Result.error(code, rs.getString("message"));
        }

        JSONObject memberMap = rs.getJSONObject("data");
        if(Objects.isEmpty(memberMap)){
            return Result.error(1,"微信用户openId缺失.");
        }

        BigDecimal amt = jsonObject.getBigDecimal("total");
        if (amt.compareTo(BigDecimal.ZERO) <= 0) {
            return Result.error(0, "总金额必须大于0");
        }

        JSONObject chargeSundry = jsonObject.getJSONObject("chargeSundry");

        ChargeRecord cr = Pool.newInstance(ChargeRecord.class);
        cr.setCommunityNo(chargeSundry.getString("communityNo"));
        cr.setCommunityName(chargeSundry.getString("communityName"));

        chargeRecordService.setDepartmentNo(cr, token.getCompanyNo());
        cr.setMbrCode(token.getUuid());
        cr.setMbrName(token.getName());
        cr.setMobile(token.getMobile());
        cr.setAvatar(token.getHeadUrl());
        cr.setOpenid(memberMap.getString("mn_open_id"));
        cr.setAmt(amt);
        cr.setTradeType("JSAPI");
        cr.setBizType(WaterSource.SUNDRY);
        
        CallBackReturn<JSONObject> called = ()->{

            JSONObject json = new JSONObject();
            json.put("companyNo", token.getCompanyNo());
            json.put("communityNo", chargeSundry.getString("communityNo"));

            return chargeRecordService.createUnifiedOrderByChargeRecord(
                    token,
                    request,
                    cr,
                    chargeSundry.getString("communityName") + "-" + chargeSundry.getString("feeTypeName"),
                    json.toJSONString(),"wxpay_mp"
            );
	    };
        JSONObject result =  Locks.exeSyn(cr.getOpenid(), called);

        if (result.getInteger("code") == 200) {
            // 微信统一下单成功，记录相关信息
            ChargeRecordTemporary chargeRecordTemporary = Pool.newInstance(ChargeRecordTemporary.class);
            chargeRecordTemporary.setCommunityNo(chargeSundry.getString("communityNo"));
            chargeRecordTemporary.setCommunityName(chargeSundry.getString("communityName"));
            chargeRecordTemporary.setTotal(amt);
            chargeRecordTemporary.setRemark(chargeSundry.getString("remark"));
            chargeRecordTemporary.setChargeFreeTypeId(chargeSundry.getString("feeTypeId"));
            chargeRecordTemporary.setChargeFreeTypeName(chargeSundry.getString("feeTypeName"));
            chargeRecordTemporary.setChargeRecordId(cr.getId());
            jdbcSession.insert(chargeRecordTemporary);
        }
        return result;

    }

    private boolean checkLogin(Token token) {
        if (Objects.isNull(token)) {
            return false;
        }

        if (Objects.isEmpty(token.getUuid()) && Objects.isEmpty(token.getUsername())) {
            return false;
        } else {
            return true;
        }
    }
}
